const XLSX = require('xlsx');
const fs = require('fs');

// Load the xlsx file
const workbook = XLSX.readFile('翻译.xlsx');

// Initialize an empty object to store the data
let sheetData = {};
// console.log('workbook', workbook);
// Iterate over each worksheet
workbook.SheetNames.forEach(sheetName => {
  // Convert each worksheet to a JSON object
  const worksheet = workbook.Sheets[sheetName];
  sheetData = XLSX.utils.sheet_to_json(worksheet);

  // console.log('sheetData', sheetData);

});
// 转换为扁平对象，key为原始的多层级路径
function convertToEnObject (array) {
  const enObject = {};
  array.forEach(item => {
    if (!item?.Keys) return;
    console.log('--', item.Keys, item.En);
    enObject[item.Keys] = item.En;
  });
  return enObject;
}

let obj = convertToEnObject(sheetData)
console.log('obj', obj)
// Write the object back to a JavaScript file
fs.writeFileSync('result.js', `module.exports = ${JSON.stringify(obj, null, 3)};`);

console.log('saved "result.js".');
